<?php
/**
 * @author Mazvv
 * @package Cities
 */
class Cities extends CRM_Controller {
	
	public function __construct(){
		parent::__construct();
		$this->a_identifier = 'cities';
		$this->lang->load('cities');
		$this->load->model('cities_model');
		$this->SetNewToolSize('600x300');
		$this->SetEditToolSize('600x300');
		$this->SetDetailsToolSize('600x300');
		$this->SetFindToolSize('600x300');
		
		
		$this->a_validation_rules['_countries_rid'] = "required";
		$this->a_validation_rules['city_name'] = "required";
		$this->a_validation_rules['city_name_lat'] = "required";
		
		$this->a_validation_fields['_countries_rid'] = $this->lang->line('COUNTRY');
		$this->a_validation_fields['city_name'] = $this->lang->line('NAME');
		$this->a_validation_fields['city_name_lat'] = $this->lang->line('NAME_LAT');
		$this->a_validation_fields['descr'] = $this->lang->line('DESCR');
		$this->a_validation_fields['archive'] = $this->lang->line('ARCHIVE');
		$this->a_validation_fields['modifyDT'] = $this->lang->line('ARCHIVE');
		$this->validation->set_rules($this->a_validation_rules);
		$this->validation->set_fields($this->a_validation_fields);
		
		
		$this->objectsList['MENU_OBJECT'] = $this->menucontainer->RenderVerticalTreeMenu();
		
	}
	
	public function _remap($m_Name){
		switch ($m_Name) {
			case 'nJ': {echo $this->_create();break;}
			case 'eJ': {echo $this->_edit();break;}
			case 'dJ': {echo $this->_details();break;}
			case 'rJ': {echo $this->_remove();break;}
			case 'fJ'; {echo $this->_find();break;}
			case 'uJ'; {echo $this->_refresh();break;}
			case 'srt'; {$this->_sort();break;}
			case 'ar'; {$this->_activerecord();break;}
			case 'sar'; {$this->_setactiverecord();break;}
			case 'pg':{echo $this->_journal();break;}
			default: $this->_index();
		}
	}
	
	protected function _index(){
		$this->objectsList['GRID_OBJECT'] = $this->_journal();
		$this->load->view('layouts/main_layout', $this->objectsList);
	}
	public function _journal(){
		
		$content = array();
		$content['title'] = $this->lang->line('DICTIONARY_TITLE'); 
		$content['rid'] = 'ID'; 
		$content['country_name'] =  $this->lang->line('COUNTRY');
		$content['city_name'] =  $this->lang->line('NAME');
		$content['city_name_lat'] =  $this->lang->line('NAME_LAT');
		$content['archive'] =  $this->lang->line('ARCHIVE');
		$content['modifyDT'] =  $this->lang->line('MODIFYDT');
		$content['yes'] = $this->lang->line('YES');
		$content['no'] = $this->lang->line('NO');
		$content['tools'] = $this->GetTools(); 
		$content['DS'] = $this->cities_model->GetDS();
		$content['pagination'] = $this->GetPagination();
		if(!$this->GetSessionData('activerecord')) $this->SetSessionData('activerecord', $content['DS'][0]->rid);
		$content['activerecord'] = $this->GetSessionData('activerecord');	
		//echo $content['activerecord'];
		return $this->load->view('cities/journal', $content, True);		
	}
	
	private function _create(){
		if ($this->validation->run() == FALSE){
			$content = array();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_CREATE')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_CREATE');
			# { city_name
			$content['city_name'] =  $this->RenderLabel('city_name');
			$content['city_name_field'] =  form_input('city_name', $this->validation->city_name?$this->validation->city_name:'');
			# } city_name
			
			# { city_name_lat
			$content['city_name_lat'] =  $this->RenderLabel('city_name_lat');
			$content['city_name_lat_field'] =  form_input('city_name_lat', $this->validation->city_name_lat?$this->validation->city_name_lat:'');
			# } city_name_lat
			
			# { _countries_rid
			$content['_countries_rid'] =  $this->RenderLabel('_countries_rid');
			$countries = array(' '=>'');
			foreach($this->cities_model->GetCountriesList() as $country){
				$countries[$country->rid] = $country->country_name;
			}
			$content['_countries_rid_field'] =  form_dropdown('_countries_rid', $countries, $this->validation->_countries_rid?$this->validation->_countries_rid:'');
			# { _countries_rid
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2', 'value'=>$this->validation->descr?$this->validation->descr:''); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:'');
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:'');
			# } modifyDT
			return $this->load->view('cities/create', $content, True);
		}
		if($insRid = $this->cities_model->CreateRecord()){
			$this->SetSessionData('activerecord', $insRid);
			$this->load->view('standart/tmpl/create_success', array('identifier'=>$this->GetIdentifier()));
		}
	}
	
	private function _edit(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		if ($this->validation->run() == FALSE){
			$content = array();
			$content['DS'] = $this->cities_model->GetEditDS();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_EDIT')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_EDIT');
			# { city_name
			$content['city_name'] =  $this->RenderLabel('city_name');
			$content['city_name_field'] =  form_input('city_name', $this->validation->city_name?$this->validation->city_name:$content['DS']->city_name);
			# } city_name
			
			# { city_name_lat
			$content['city_name_lat'] =  $this->RenderLabel('city_name_lat');
			$content['city_name_lat_field'] =  form_input('city_name_lat', $this->validation->city_name_lat?$this->validation->city_name_lat:$content['DS']->city_name_lat);
			# } city_name_lat
			
			# { _countries_rid
			$content['_countries_rid'] =  $this->RenderLabel('_countries_rid');
			$countries = array(' '=>'');
			foreach($this->cities_model->GetCountriesList() as $country){
				$countries[$country->rid] = $country->country_name;
			}
			$content['_countries_rid_field'] =  form_dropdown('_countries_rid', $countries, $this->validation->_countries_rid?$this->validation->_countries_rid:$content['DS']->_countries_rid);
			# } _countries_rid
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2', 'value'=>$this->validation->descr?$this->validation->descr:''); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:'');
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:'');
			# } modifyDT
			return $this->load->view('cities/edit', $content, True);
		}
		if($this->cities_model->UpdateRecord()){
			$this->load->view('standart/tmpl/edit_success', array('identifier'=>$this->GetIdentifier()));
		}
	}
	
	private function _details(){
			if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
			$content = array();
			$content['DS'] = $this->cities_model->GetEditDS();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_EDIT')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_EDIT');
			# { city_name
			$content['city_name'] =  $this->lang->line('NAME');
			$content['city_name_field'] =  form_input('city_name', $this->validation->city_name?$this->validation->city_name:$content['DS']->city_name);
			# } city_name
			
			# { city_name_lat
			$content['city_name_lat'] =  $this->lang->line('NAME_LAT');
			$content['city_name_lat_field'] =  form_input('city_name_lat', $this->validation->city_name_lat?$this->validation->city_name_lat:$content['DS']->city_name_lat);
			# } city_name_lat
			
			# { _countries_rid
			$content['_countries_rid'] =  $this->lang->line('COUNTRY');
			$countries = array(' '=>'');
			foreach($this->cities_model->GetCountriesList() as $country){
				$countries[$country->rid] = $country->country_name;
			}
			$content['_countries_rid_field'] =  form_dropdown('_countries_rid', $countries, $this->validation->_countries_rid?$this->validation->_countries_rid:$content['DS']->_countries_rid);
			# { _countries_rid
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2', 'value'=>$this->validation->descr?$this->validation->descr:''); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:'');
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:'');
			# } modifyDT
			return $this->load->view('cities/details', $content, True);
	}

	private function _remove(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		$this->cities_model->DeleteRecord();
		$this->SetSessionData('activerecord', null);
		echo $this->_journal();
	}
	
	private function _find(){
		if (!isset($_POST['goFind'])){
			$content = array();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_FIND')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_FIND');

			# { city_name
			$content['city_name'] =  $this->lang->line('NAME');
			$content['city_name_field'] =  form_input('city_name');
			# } city_name
			
			# { city_name_lat
			$content['city_name_lat'] =  $this->lang->line('NAME_LAT');
			$content['city_name_lat_field'] =  form_input('city_name_lat');
			# } city_name_lat
			
			# { _countries_rid
			$content['_countries_rid'] =  $this->lang->line('COUNTRY');
			$countries = array(' '=>'');
			foreach($this->cities_model->GetCountriesList() as $country){
				$countries[$country->rid] = $country->country_name;
			}
			$content['_countries_rid_field'] =  form_dropdown('_countries_rid', $countries);
			# { _countries_rid
									
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2'); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array(' '=>'', '0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')));
			# } archive
			
			return $this->load->view('cities/find', $content, True);			
		}
		$searchRule = array();
		if(isset($_POST['city_name']) && trim($_POST['city_name'])) $searchRule['_cities.city_name'] = trim($_POST['city_name']);
		if(isset($_POST['city_name_lat']) && trim($_POST['city_name_lat'])) $searchRule['_cities.city_name_lat'] = trim($_POST['city_name_lat']);
		if(isset($_POST['_countries_rid']) && trim($_POST['_countries_rid'])) $searchRule['_cities._countries_rid'] = trim($_POST['_countries_rid']);
		if(isset($_POST['descr']) && trim($_POST['descr'])) $searchRule['_users.descr'] = trim($_POST['descr']);
		if(isset($_POST['archive']) && trim($_POST['archive'])) $searchRule['_users.archive'] = trim($_POST['archive']);
		if($searchRule){
			$this->_setsearchrule($searchRule);
		}
		if($_POST['goFind']=='2'){
			redirect('cities', 'refresh');
		}
		$this->load->view('standart/tmpl/find_success', array('identifier'=>$this->GetIdentifier()));
	}
	
	private function _refresh(){
		$this->RefreshSessionData();
		echo $this->_journal();
	}
	
}

?>